//
//  BSBTabbar.m
//  Baisebudejie
//
//  Created by apple on 16/3/14.
//  Copyright © 2016年 youminuo. All rights reserved.
//

#import "BSBTabbar.h"

@interface BSBTabbar ()

@property (weak, nonatomic) UIButton *addBtn;

@end

@implementation BSBTabbar

/**
 *  初始化
 */
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.backgroundImage = [UIImage imageNamed:@"tabbar-light"];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateSelected];
        [self addSubview:btn];
        [btn addTarget:self action:@selector(modelView) forControlEvents:UIControlEventTouchUpInside];
        self.addBtn = btn;
    }
    return self;
}

/**
 *  中间按钮点击事件
 */
- (void)modelView
{
    if (self.modelBlock)
    {
        self.modelBlock();
    }
}


/**
 *  重新给按钮布局
 */
- (void)layoutSubviews
{
    [super layoutSubviews];
    [self.addBtn sizeToFit];
    self.addBtn.center = CGPointMake(self.width * 0.5, self.height * 0.5);
    CGFloat btnX = 0;
    CGFloat btnY = 0;
    CGFloat btnW = self.width *0.2;
    CGFloat btnH = self.height;
    NSInteger index = 0;
    
    /**
     *  找出所有自控制的item 重新设置frame
     */
    for (UIView *btn in self.subviews)
    {
        if (!([btn isKindOfClass:[UIControl class]]) || btn == self.addBtn)
        {
            continue;
        }
        btnX = btnW * ((index > 1)?(index + 1):index);
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
        index++;
    }
}

@end
